package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFieldRelevanceDao;
import com.slipper.system.codeless.bo.DataFieldRelevanceBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFieldRelevanceService {
    @Resource
    private DataFieldRelevanceDao dataFieldRelevanceDao;
    
    public DataFieldRelevanceBO queryBy(String id) {
        return dataFieldRelevanceDao.queryBy(id);
    }
    
    public List<DataFieldRelevanceBO> queryList(DataFieldRelevanceBO bo) {
        return dataFieldRelevanceDao.queryList(bo);
    }
    
    public PageInfo<DataFieldRelevanceBO> queryPage(DataFieldRelevanceBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFieldRelevanceDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String fieldId, List<DataFieldRelevanceBO> list, String employeeId) {
        if (Tool.isBlank(fieldId)) {
            return 0;
        }

        // 保存/更新数据
        int i = 0;
        for (DataFieldRelevanceBO relevance : list) {
            relevance.setFieldId(fieldId);
            relevance.setOrdinal(++i);
            if (Tool.isBlank(relevance.getId())) {
                relevance.setId(UUID.getUUID());
                relevance.setUpdateId(employeeId);
                relevance.setCreateId(employeeId);
                relevance.setUpdateTime(System.currentTimeMillis());
                relevance.setCreateTime(System.currentTimeMillis());
                dataFieldRelevanceDao.insert(relevance);
            } else {
                relevance.setUpdateId(employeeId);
                relevance.setUpdateTime(System.currentTimeMillis());
                dataFieldRelevanceDao.update(relevance);
            }
        }

        // 删除被删除的数据
        DataFieldRelevanceBO qo = new DataFieldRelevanceBO();
        qo.setFieldId(fieldId);
        dataFieldRelevanceDao.queryList(qo).stream()
                .filter(relevance -> list.stream().noneMatch(temp -> relevance.getId().equals(temp.getId())))
                .forEach(this::delete);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFieldRelevanceBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFieldRelevanceDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFieldRelevanceDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFieldRelevanceDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(DataFieldRelevanceBO bo) {
        return dataFieldRelevanceDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByFieldId(String fieldId) {
        DataFieldRelevanceBO qo = new DataFieldRelevanceBO();
        qo.setFieldId(fieldId);
        dataFieldRelevanceDao.queryList(qo).forEach(this::delete);
    }

    public List<DataFieldRelevanceBO> queryFieldId(String fieldId) {
        if (Tool.isBlank(fieldId)) {
            return Collections.emptyList();
        }

        DataFieldRelevanceBO qo = new DataFieldRelevanceBO();
        qo.setFieldId(fieldId);
        return dataFieldRelevanceDao.queryList(qo);
    }
}
